package com.tmc.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.tmc.bean.common.CommonResult;
import com.tmc.bean.common.ContextInfo;
import com.tmc.bean.common.Pagination;
import com.tmc.entity.baseinfo.City;
import com.tmc.service.baseinfo.CityServiceImpl;
import com.tmc.web.BaseController;

@Controller
@RequestMapping("/city")
public class CityController extends BaseController {

	@Autowired
	private CityServiceImpl service;
	
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public ModelAndView findist(City cond, Integer pageIndex, Integer pageSize, HttpServletRequest request) {
		ContextInfo context = this.getContextInfo(request);
		if (pageIndex == null || pageIndex < 1)
			pageIndex = 1;
		if (pageSize == null || pageIndex < 1 || pageIndex > 1000)
			pageSize = 10;
		Map<String, Object> map = new HashMap<String, Object>();
		Pagination<City> page = service.findByCon(context, cond, pageIndex, pageSize);
		map.put("page", page);
		map.put("cond", cond);
		map.put("context", context);
		return new ModelAndView("views/baseInfo/city-list", map);
	}
	
	@RequestMapping(value = "/all", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String findAll() {
		List<City> all = service.findAllFromCache(new ContextInfo());
		return JSON.toJSONString(all);
	}
	
	@RequestMapping(value = "/editPage")
	public ModelAndView editPage(Long cityId, HttpServletRequest request) {
		ContextInfo context = this.getContextInfo(request);
		Map<String, Object> map = new HashMap<String, Object>();
		City city = null;
		if (cityId != null)
			city = service.getById(context, cityId);
		else
			city = new City();
		map.put("city", city);
		map.put("context", context);
		return new ModelAndView("views/baseinfo/city-edit", map);
	}
	
	@RequestMapping(value = "/save")
	@ResponseBody
	public String save(City city) {
		CommonResult cr = service.save(new ContextInfo(), city);
		return cr.toJson();
	}
	
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	@ResponseBody
	public String delete(Long cityId) throws Exception {
		CommonResult cr = service.delete(new ContextInfo(), cityId);
		return cr.toJson();
	}
	
	@RequestMapping(value = "/updatefromairport", method = RequestMethod.POST)
	public ModelAndView updatefromairport(HttpServletRequest request) throws Exception {
		ContextInfo context = this.getContextInfo(request);
		Map<String, Object> map = new HashMap<String, Object>();
		service.updateFromAirport(context);
		map.put("cond", new City());
		map.put("page", new Pagination<City>());
		map.put("context", context);
		return new ModelAndView("views/baseInfo/city-list", map);
	}
	
	@RequestMapping(value = "/updateself", method = RequestMethod.POST)
	public ModelAndView updateself(HttpServletRequest request) throws Exception {
		ContextInfo context = this.getContextInfo(request);
		Map<String, Object> map = new HashMap<String, Object>();
		service.updateSelf(context);
		map.put("cond", new City());
		map.put("page", new Pagination<City>());
		map.put("context", context);
		return new ModelAndView("views/baseInfo/city-list", map);
	}
	
	@RequestMapping(value = "/updatefromcountry", method = RequestMethod.POST)
	public ModelAndView updatefromcountry(HttpServletRequest request) throws Exception {
		ContextInfo context = this.getContextInfo(request);
		Map<String, Object> map = new HashMap<String, Object>();
		service.updateFromCountry(context);
		map.put("cond", new City());
		map.put("page", new Pagination<City>());
		map.put("context", context);
		return new ModelAndView("views/baseInfo/city-list", map);
	}
	
	@RequestMapping(value="/searchCity", method = RequestMethod.POST)
	@ResponseBody
	public String searchCity(String query){
		List<City> citys = service.searchCity(query);
		JSONArray array = new JSONArray();
		for (City city : citys) {
			JSONObject obj = new JSONObject();
			obj.put("Key", city.getCityCode());
			obj.put("Name", city.getCityName()+"("+city.getCityCode()+")");
			array.add(obj);
		}
		return array.toJSONString();		
	}
}
